c   Program name: MEAggre.for computes distributions of aggregate

c     totals.

c    Uses sample numbers from MESamp.for and aggregates oil

c     and gas in-place or recoverable resources for ANWR plays

c     Aggregation is by total area, 1002, undeformed in 1002, and

c     deformed in 1002.

c

c    Written 4/2/98, Schuenemeyer	 

c

c    Input files:

c     Unit  8 - Play file, name obtained from Names.dat

c     Unit  9 - Rand.dat, index numbers of data to be sampled

c                 to obtain correlation

c     Unit 10 - Names.dat, Names of play and prospect files, number of

c                 replications.

c    Output files:

c     Unit 11 - AnwrD or AnwrIP, recoverable or in-place aggregate

c                 files.

c

      parameter(epsilon=0.001,lx=50)

      character title*70

	character pln(11)*25,ppl(11)*15,ppr(11)*15

      dimension xo(10000,11),xg(10000,11),nply(11),nud(11),npro(11)

     1 ,xso(11),xsg(11),xia(11),so(4),sg(4),tmp(10)	      

      write(*,2)

    2 format(' Program MEAggre.for - Aggregate Distributions'/)

      open(9,file='Rand.dat',status='old')

      open(10,file='Names.dat',status='old')

    4	write(*,*) ' Aggregate in-place or recoverable. Enter 1 or 2)'

	read(*,*) ipr

	if (ipr.lt.1.or. ipr.gt.2) go to 4

	if(ipr.eq.2) then

	 open(11,file='AnwrD.dat')

	else

	 open(11,file='AnwrIP.dat')

	end if

c   num is number of successful plays. Corresponds to number of 

c    observtions in the play files.  Note that Rand.dat also

c    contains 10,000 sample numbers.

	num=10000

	read(10,'(a70)') title

	read(10,*) np

	do i = 1,np

c   num of plays, undef(1)/def(2) code, percent resource in 1002

	 read(10,*)id,nply(i),nud(i),npro(i)

	 nply(i)=nply(i)-num

c   play name, name of play file, name of prospect file

	 read(10,'(3x,a25/3x,a15/3x,a15)')pln(i),ppl(i),ppr(i)

	end do

c   read play file

	do i=1,np

	 open(8,file=ppl(i),status='old')

	 read(8,*)

	  do m=1,num

	   read(8,*) kd,(tmp(k),k=1,10)

	   if(ipr.eq.2) then

c   recoverable

	    xo(m,i)=tmp(1)

	    xg(m,i)=tmp(3)

	   else

c   in place

	    xo(m,i)=tmp(8)

	    xg(m,i)=tmp(10)

	   end if

	  end do

	 close (8)

	end do

c   sample

      write(11,47)

   47 format(' TotalOil TotalNAGas 1002Oil 1002NAGas Undef1002Oil',

     1 ' Undef1002NAGas Def1002Oil Def1002NAGas')

      read(9,*)

	do m=1,num

	  read(9,*)id,(xia(j),j=1,np)

c   retrieve sample

	 do j=1,np

	  na=int(xia(j)+.001)

	  nar=na-nply(j)

	  if(nar.le.0) then

	   xso(j)=0.0

	   xsg(j)=0.0

	  else

c   xso is oil in mm bbl

c   xgo is na gas in b cf

	   xso(j)=xo(nar,j)

	   xsg(j)=xg(nar,j)

	  end if

	 end do

	

c   partition sample & get sums

      np1=np-1

	do k=1,4

	 so(k)=0.0

	 sg(k)=0.0

	end do

c   weight Nig/Aurora (2 large) and Nig/Aurora (many)

	 xso(np1)=0.3*xso(np1)+0.7*xso(np)

	 xsg(np1)=0.3*xsg(np1)+0.7*xsg(np)

	do j=1,np1

c   all ANWR

	 so(1)=so(1)+xso(j)

	 sg(1)=sg(1)+xsg(j)

	 t=real(npro(j))*0.01

	 to=xso(j)*t

	 tg=xsg(j)*t

c   within 1002

	 so(2)=so(2)+to

	 sg(2)=sg(2)+tg

	 if(nud(j).eq.1) then

c   within undeformed

	  so(3)=so(3)+to

	  sg(3)=sg(3)+tg

	 else

c   withing deformed

	  so(4)=so(4)+to

	  sg(4)=sg(4)+tg

	 end if

	end do

c   write aggregate totals

	write(11,72) m,(so(k),sg(k),k=1,4)

   72 format(i6,8f15.4)

	end do

      stop

      end

